import brightness from '@blueos.hardware.display.brightness'
import file from '@blueos.storage.file'

export const writeText = (json) => {
  file.writeText({
    uri: 'internal://files/result.txt',
    text: JSON.stringify(json),
    success: function () {
      console.log('handling success')
    },
    fail: function (data, code) {
      console.log(`handling fail, code = ${code}`)
    },
  })
}

export const getLastResult = () => {
  // @ts-ignore
  return new Promise((reslove, reject) => {
    file.readText({
      uri: 'internal://files/result.txt',
      success: function (data) {
        console.log(data.text)
        try {
          // @ts-ignore
          reslove(JSON.parse(data.text))
        } catch (err) {
          reslove({})
        }
      },
      fail: function (data, code) {
        console.log(`handling fail, code = ${code}`)
        reslove({})
      },
    })
  })
}




export const setKeepScreenOn = () => {
  brightness.setKeepScreenOn({
    // @ts-ignore
    keepScreenOn: true,
    success: function () {
      console.log('handling success')
    },
    fail: function (data, code) {
      console.log(`handling fail, code = ${code}`)
    },
  })
}


export const setKeepScreenOff = () => {
  brightness.setKeepScreenOn({
    // @ts-ignore
    keepScreenOn: false,
    success: function () {
      console.log('handling success')
    },
    fail: function (data, code) {
      console.log(`handling fail, code = ${code}`)
    },
  })
}
